<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>商品备案信息详细</title>
    <%@include file="/WEB-INF/pages/common/taglib.jsp" %>
    <%@include file="/WEB-INF/pages/common/common.jsp" %>
    <style>
        #attach_form input {
            width: 90% !important;
        }

        #attach_form textarea {
            max-width: 2000px;
            width: 88% !important;
        }
    </style>
</head>
<body>
<div class="content content_frame">
    <h3 class="mainTitle"><img src="${basePath}/images/icon2.png"/>商品备案信息详细
    </h3>

    <c:set var="isEditablePage" value="false" scope="request"></c:set>
    <c:if test="${empty headInsp.status
                                   || 'E0' eq headInsp.status
                                   || '4' eq headInsp.status
                                   || '7' eq headInsp.status
                                   || '9' eq headInsp.status}">
        <c:set var="isEditablePage" value="true" scope="request"></c:set>
    </c:if>

    <br/>
    <ul class="contentNav dib-wrap">
        <c:if test="${isEditablePage}">
            <li class="dib">
                <a id="btn_basic_declare">
                    <i class='fa fa-book'></i>&nbsp;&nbsp;备&nbsp;案 </a>
            </li>

            <li class="dib">
                <a id="btn_basic_save">
                    <i class='fa fa-save'></i>&nbsp;&nbsp;保&nbsp;存 </a>
            </li>
        </c:if>
        <li class="dib" onclick="toInit()">
            <a>
                <i class='fa fa-reply-all'></i>&nbsp;&nbsp;返&nbsp;回 </a>
        </li>
    </ul>
    <br/>

    <form id="basic_form">
        <div class="title">第1项 商品基本信息
            <div class="div-status-display" style="float: right;margin-right: 50px;font-size: 16px;font-weight: bold;">
                <span><i class="icon-bookmark text-blue"></i>状态: </span>
                    <span style="cursor: pointer" id="statusDisplay">
                        ${empty headInsp.statusDisplay ? '暂无' : headInsp.statusDisplay}
                    </span>
            </div>
        </div>
        <input type="hidden" id="id" name="id"
               value="${headInsp.id}">
        <table class="table3">
            <tr>
                <td width="15%">商品备案流水号</td>
                <td width="35%">
                    <label><input type="text" id="goodsserialno" name="goodsserialno" readonly
                                  value="${headInsp.goodsserialno}"></label>
                </td>
                <td width="15%">企业备案号</td>
                <td width="35%">
                    <label><input type="text" id="entcode" name="entcode"
                                  value="${headInsp.entcode}"></label>
                </td>
            </tr>
            <tr>
                <td>申报单位代码</td>
                <td>
                    <label> <input type="text" id="declentcode" name="declentcode"
                                   value="${headInsp.declentcode}"></label>
                </td>
                <td>商品大类</td>
                <td>
                    <label>
                        <ui:tsingletableautocomplete
                                keyFieldName="goodscategories"
                                valueFieldName="goodscategoriesXXXX"
                                tableName="DICT_ITEM"
                                key="ITEM_KEY"
                                value="ITEM_VALUE"
                                groupId="GOODS_CATEGORIES"
                                defaultKey="${headInsp.goodscategories}"
                                defaultValue=""
                                /> </label>
                </td>
            </tr>
            <tr>
                <td>进出口标志</td>
                <td>
                    <label><ui:tsingletableautocomplete
                            keyFieldName="ieflag"
                            valueFieldName="ieflagXXXX"
                            tableName="DICT_ITEM"
                            key="ITEM_KEY"
                            value="ITEM_VALUE"
                            groupId="ieType"
                            defaultKey="${headInsp.ieflag}"
                            defaultValue=""
                            /></label>
                </td>
                <td>HS编码</td>
                <td>
                    <label> <ui:tsingletableautocomplete
                            keyFieldName="hscode"
                            valueFieldName="hscodeXXXX"
                            tableName="S_HSCODE"
                            key="HS_CODE"
                            value="HS_CNAME"
                            defaultKey="${headInsp.hscode}"
                            defaultValue="${headInsp.hscode}"
                            isDisplayKey="true"
                            onchange="setGoodsName"/></label>
                </td>
            </tr>
            <tr>
                <td>商品名称</td>
                <td>
                    <label> <input type="text" id="goodsname" name="goodsname"
                                   value="${headInsp.goodsname}"></label>
                </td>
                <td>商品货号</td>
                <td>
                    <label> <input type="text" id="goodsskuno" name="goodsskuno"
                                   value="${headInsp.goodsskuno}"></label>
                </td>
            </tr>
            <tr>
                <td>主要成分</td>
                <td>
                    <label> <input type="text" id="masterbase" name="masterbase"
                                   value="${headInsp.masterbase}"></label>
                </td>
                <td>规格型号</td>
                <td>
                    <label> <input type="text" id="spec" name="spec"
                                   value="${headInsp.spec}"></label>
                </td>
            </tr>
            <tr>
                <td>生产国</td>
                <td>
                    <label> <ui:tsingletableautocomplete keyFieldName="origincountry"
                                                         valueFieldName="origincountryXXX"
                                                         tableName="S_ORIGINAL_PLACE_E"
                                                         key="CODE_I"
                                                         value="NAME_I"
                                                         defaultKey="${headInsp.origincountry}"
                                                         defaultValue=""
                            /></label>
                </td>
                <td>生产企业</td>
                <td>
                    <label> <input type="text" id="proent" name="proent"
                                   value="${headInsp.proent}"></label>
                </td>
            </tr>
            <tr>
                <td>产品品牌</td>
                <td>
                    <label> <input type="text" id="brand" name="brand"
                                   value="${headInsp.brand}"></label>
                </td>
                <td>供应商</td>
                <td>
                    <label> <input type="text" id="supplier" name="supplier"
                                   value="${headInsp.supplier}"></label>
                </td>
            </tr>
            <tr>
                <td>最小包装单位</td>
                <td>
                    <label><ui:tsingletableautocomplete
                            tableName="S_LAW_UNIT_E"
                            key="CODE_I"
                            value="NAME_I"
                            keyFieldName="smallqtyunitcode"
                            valueFieldName="smallqtyunitcodeXXX"
                            defaultKey="${headInsp.smallqtyunitcode}"
                            defaultValue=""
                            /></label>
                </td>

                <td>用途</td>
                <td>
                    <label> <ui:tsingletableautocomplete
                            key="PURPOSECO"
                            value="PURPOSECN"
                            tableName="S_PURPOSE"
                            defaultKey="${headInsp.useway}"
                            defaultValue=""
                            keyFieldName="useway"
                            valueFieldName="usewayXXX"
                            /></label>
                </td>
            </tr>

            <tr>
                <td>符合国家法规</td>
                <td>
                    <label> <ui:tsingletableautocomplete
                            keyFieldName="accordwith"
                            valueFieldName="accordwithXXXX"
                            tableName="DICT_ITEM"
                            key="ITEM_KEY"
                            value="ITEM_VALUE"
                            groupId="YesOrNo"
                            defaultKey="${headInsp.accordwith}"
                            defaultValue=""
                            /></label>
                </td>

                <td>业务类型</td>
                <td>
                    <label> <ui:tsingletableautocomplete
                            keyFieldName="biztypexx"
                            valueFieldName="biztypeXXXX"
                            tableName="DICT_ITEM"
                            key="ITEM_KEY"
                            value="ITEM_VALUE"
                            groupId="goodsBackupBizType"
                            defaultKey="${'Y' eq headInsp.biztypei ? 'direct' : ('Y' eq headInsp.biztypec ? 'bond' : '')}"
                            defaultValue=""
                            onchange="changeBiztype"
                            />
                        <input type="hidden" id="biztypei" name="biztypei"
                               value="${headInsp.biztypei}">
                        <input type="hidden" id="biztypec" name="biztypec"
                               value="${headInsp.biztypec}"></label>
                </td>
            </tr>
            <tr>
                <td> 申报时间</td>
                <td>
                    <label><input type="text" name="applytime" id="applytime" readonly
                                  value="${headInsp.applytime}"></label>
                </td>

                <td>备案号</td>
                <td>
                    <label> <input type="text" id="backupNo" name="backupNo" readonly
                                   value="${headInsp.backupNo}"></label>
                </td>
            </tr>
        </table>
        <br/>
    </form>
    <div class="title">第2项 商品附件信息 <c:if test="${isEditablePage}">
        <div id="deleteAttachBtn" class="divBtn" style="margin-top: -6px">
            <span style="margin: 0 auto"><i class="fa fa-remove" style="margin-top: 12px;"></i><span
                    style="color: #555 ">&nbsp;&nbsp;删&nbsp;除</span> </span>
        </div>
    </c:if></div>
    <div style="margin:15px 5px 0 0;">
        <table id="attachTable" style="height: 1px"></table>
        <div id="attachPaper">
        </div>
    </div>
    <c:if test="${isEditablePage}">
        <br/>

        <div class="title">第3项 商品附件上传
            <div id="saveAttachBtn" class="divBtn" style="margin-top: -6px">
                    <span style="margin: 0 auto"><i class="fa fa-save"
                                                    style="margin-top: 12px;"></i><span style="color: #555 ">&nbsp;&nbsp;保&nbsp;存</span></span>
            </div>
        </div>
        <div style="margin:0px 0px 0 0;">
            <form id="attach_form">
                <input type="hidden" name="relateId" value="${headInsp.id}">
                <input type="hidden" name="id" value="">

                <table class="table3" width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tbody>
                    <tr>
                        <td>
                            附件类型
                        </td>
                        <td colspan="2">
                            <label><ui:tsingletableautocomplete
                                    keyFieldName="annextype"
                                    valueFieldName="annextypeXXXX"
                                    tableName="DICT_ITEM"
                                    key="ITEM_KEY"
                                    value="ITEM_VALUE"
                                    groupId="goodsAttachType"
                                    defaultKey="${attach.annextype}"
                                    defaultValue=""/>
                                <input type="hidden" name="fileId" value="${attach.fileId}"></label>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            上传附件
                        </td>
                        <td colspan="2">
                            <label><input type="text" name="annexname" readonly="readonly" style="width: 70%"
                                          value="${attach.annexname}"></label>
                            <table class="fileTable" width="100%" border="0" cellspacing="0"
                                   cellpadding="0"
                                   style="margin-right: -15px;margin-top: -30px;height: 10px !important;">
                                <tbody id="filetable">
                                <tr>
                                </tr>
                                </tbody>
                            </table>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            备&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;注
                        </td>
                        <td colspan="2">
                            <label><textarea name="remark" value="${attach.remark}"
                                    ></textarea></label>
                        </td>
                    </tr>
                    </tbody>
                </table>
                <input type="hidden" id="attachFormHidden" value="0">
            </form>
        </div>
    </c:if>
</div>
</body>
<script type="text/javascript">
    var editable = ${isEditablePage};
    function toInit() {
        location.href = "${basePath}/goodsManage/goodsBackupInsp/init";
    }

    // 初始化加载列表信息
    $(function () {
        popStatus("statusDisplay",
                "${basePath}/status/getInspStatus",
                {
                    "receiptNo": $("#basic_form input[name='goodsserialno']").val(),
                    "statusGroupId": "goodsBackupStatus"
                }
        );

        if (editable) {
            zzupload("filetable", function (data) {
                $("input[name='annexname']").val(data.fileName);
                $("input[name='fileId']").val(data.fileId);
            }, true);
        }
        else {
            $("button:not(#btn_back)").css({"display": "none", "disabled": "disabled"});
            $(":text,:checkbox,:radio").attr("disabled", "disabled");
        }

        $("#attachTable").jqGrid({
            datatype: "json",
            url: $basePath + "/goodsManage/goodsBackupInsp/pageAttach",
            postData: {"relateId": $("#basic_form input[name='id']").val()}, //发送数据
            colNames: ['流水号',
                '文件名',
                '附件类型',
                '备注',
                '入库时间',
                ''
            ],
            colModel: [{name: 'id', index: 'id', hidden: true},
                {
                    name: 'annexname', index: 'annexname', formatter: function (cellvalue, options, rowObject) {
                    return "<a class='btn-link' href='#download' onclick=\"fileDownload('" + rowObject.fileId + "')\"><span style='color:#00acec'>" + rowObject.annexname + "</span></a>"
                }
                },
                {name: 'annextypeDisplay', index: 'annextypeDisplay'},
                {name: 'remark', index: 'remark'},
                {name: 'createTime', index: 'createTime'},
                {name: 'fileId', index: 'fileId', hidden: true}
            ],
            height: 'auto',
            shrinkToFit: false,
            autoScroll: true,
            rowNum: 5, //每页显示记录数
            rowList: [5, 10, 20], //可调整每页显示的记录数
//            rownumbers: true,
            multiselect: editable, //是否支持多选
            multiselectWidth: 35,
            viewrecords: true, //是否显示行数
            pager: "#attachPaper", //分页工具栏
            altRows: true,
            altclass: 'someClass',
            sortable: true,
            sortname: 'createTime',
            sortorder: 'asc',
            width: $('body').width(),
            loadComplete: function () {
                var rowNum = $(this).jqGrid('getGridParam', 'records');
                if (rowNum <= 0) {
                    if ($(this).parent().children('.norecords').length == 0)
                        $(this).parent().append("<div class=\"norecords\">暂无数据！</div>");
                    $(this).parent().children('.norecords').show();
                } else {//如果存在记录，则隐藏提示信息。
                    $("#attachFormHidden").val("1");
                    $(this).parent().children('.norecords').hide();
                }
                $("#" + this.id).setGridWidth($(window).width() * 0.98);
//                $("#" + this.id).setGridWidth($(".responsive-table").parent().width() - 2);
                $(".ui-jqgrid tr.ui-row-ltr td").css('text-align', 'center');
//                $("body").height($("body").height() + $(".responsive-table:eq(1)").height() + 50);
            }
        });
    });

    // 初始化触发事件
    $(function () {
        $("#btn_basic_declare").click(function () {
            if (!isValid('basic_form')) {
                return false;
            }
            if ($("#attachFormHidden").val() != '1') {
//                if (!isValid('attach_form')) {
//                    return false;
//                }
                layerAlert("请先上传附件信息并保存!");
                return false;
            }

            $.ajax({
                url: $basePath + "/goodsManage/goodsBackupInsp/saveHead",
                type: "post",
                dataType: "json",
                data: $("#basic_form").serialize(),
                async: false,
                success: function () {
                    $.ajax({
                        url: $basePath + "/goodsManage/goodsBackupInsp/declare",
                        type: "post",
                        dataType: "json",
                        data: {"id": $("#basic_form input[name='id']").val()},
                        async: false,
                        success: function (result) {
                            if (result['code'] + '' == '1') {
                                layerAlertOk("备案数据已提交申请！", function () {
                                    location.href = "${basePath}/goodsManage/goodsBackupInsp/initEdit?id=" + $("#basic_form input[name='id']").val() + "&r=" + Math.random();
                                });
                                return;
                            }


                            layerAlert("备案失败" + result['msg']);
                        },
                        error: function () {
                            layerAlert("商品备案操作失败！");
                        }
                    });
                },
                error: function () {
                    layerAlert("商品信息保存失败！");
                }
            });

        });

        $("#btn_basic_save").click(function () {
//            if (!isValid('basic_form')) {
//                return false;
//            }

            $.ajax({
                url: $basePath + "/goodsManage/goodsBackupInsp/saveHead",
                type: "post",
                dataType: "json",
                data: $("#basic_form").serialize(),
                success: function (data) {
                    if (data) {
                        layerAlertOk("商品信息保存成功！", function () {
                            location.href = "${basePath}/goodsManage/goodsBackupInsp/initEdit?id=" + data.id + "&r=" + Math.random();
                        });
                    } else {
                        layerAlert("商品信息保存失败！");
                    }
                },
                error: function () {
                    layerAlert("商品信息保存失败！");
                }
            });

        });

        $("#saveAttachBtn").click(function () {
            var id = $("#basic_form input[name='id']").val();
            if ("" == id) {
                layerAlert("请先保存商品基本信息！");
                return;
            }

            if (!isValid('attach_form')) {
                return false;
            }

            $.ajax({
                url: $basePath + "/goodsManage/goodsBackupInsp/saveAttach",
                type: "post",
                dataType: "json",
                data: $("#attach_form").serialize(),
                success: function (data) {
                    if (data) {
                        $("#attachFormHidden").val("1");
                        layerAlertOk("商品附件信息保存成功！", function () {
                            reloadGrid("attachTable", "pageAttach");
                            $("#attach_form input:not([name='relateId'])").val("");
                            $("#attach_form textarea").val("");

                        });

                    } else {
                        layerAlert("商品附件信息保存失败！");
                    }
                },
                error: function () {
                    layerAlert("商品附件信息保存失败！");
                }
            });
        });

        $("#deleteAttachBtn").click(function () {
            deleteData("attachTable", $basePath + "/goodsManage/goodsBackupInsp/deleteAttach",
                    $basePath + "/goodsManage/goodsBackupInsp/pageAttach");
        });
    });


    function getId(gridId) {
        var rowKey = $("#" + gridId + "").getGridParam("selrow");
        if (!rowKey) {
            layerAlert("对不起,您没有选中任何记录");
        } else {
            var selectedIDs = $("#" + gridId + "").getGridParam("selarrrow");
            var jsonArr = new Array();
            for (var i = 0; i < selectedIDs.length; i++) {
                jsonArr[i] = $("#" + gridId + "").jqGrid("getRowData", selectedIDs[i]);
            }
            return jsonArr;
        }
    }


    function reloadGrid(gridId, url) {
        $("#" + gridId).jqGrid('setGridParam', {
            url: url,
            postData: {"relateId": $("#basic_form input[name='id']").val()},
            page: 1
        }).trigger("reloadGrid");
    }

    function deleteData(gridId, deleteUrl, refreshUrl, callback) {
        var selectedId = $("#" + gridId).jqGrid("getGridParam", "selrow");
        if (!selectedId) {
            layerAlert("对不起,您没有选中任何记录");
            return;
        }
        showConfirm("确定要删除选中的记录吗?", "系统提示", function () {
            var selectedRowIds = $("#" + gridId).jqGrid("getGridParam", "selarrrow");
            var len = selectedRowIds.length;
            var jsonSeqId = "";
            for (var i = 0; i < len; i++) {
                jsonSeqId += $("#" + gridId).jqGrid("getRowData", selectedRowIds[i]).id;
                jsonSeqId += ",";
            }
            jsonSeqId = jsonSeqId.substr(0, jsonSeqId.length - 1);
            $.ajax({
                type: "post",
                async: false,
                dataType: "json",
                url: deleteUrl,
                data: {
                    "id": jsonSeqId + "",
                    "relateId": $("#basic_form input[name='id']").val()
                },
                success: function (result) {
                    if (result) {
                        layerAlertOk('删除成功!', function () {
                            reloadGrid(gridId, refreshUrl);
                        });
                        if (typeof callback === "function") {
                            callback();
                        }
                    } else {
                        layerAlert("删除失败", "系统提示");
                    }
                }
            });
        })
    }

    function changeBiztype(key) {
        if ('direct' == key) {
            $("#biztypei").val('Y');
            $("#biztypec").val('N');
        }
        else if ('bond' == key) {
            $("#biztypec").val('Y');
            $("#biztypei").val('N');
        }
        else {
            $("#biztypec").val('N');
            $("#biztypei").val('N');
        }
    }

    function setGoodsName(key, value) {
        $('#goodsname').val(value);
    }

    $(function () {
        var rules = {
            goodsserialno: {minlength: 1, maxlength: 50},
            entcode: {minlength: 1, maxlength: 30},
            declentcode: {minlength: 1, maxlength: 30},
            goodscategories: {minlength: 1, maxlength: 20},
            ieflag: {minlength: 1, maxlength: 1},
            hscode: {minlength: 1, maxlength: 50},
            goodsskuno: {minlength: 1, maxlength: 50},
            goodsname: {minlength: 1, maxlength: 500},
            masterbase: {minlength: 1, maxlength: 1500},
            spec: {minlength: 1, maxlength: 500},
            origincountry: {minlength: 1, maxlength: 100},
            proent: {minlength: 1, maxlength: 500},
            brand: {minlength: 1, maxlength: 500},
            supplier: {minlength: 1, maxlength: 500},
            smallqtyunitcode: {minlength: 1, maxlength: 4},
            useway: {minlength: 1, maxlength: 500},
            accordwith: {minlength: 1, maxlength: 1},
            biztypexx: {minlength: 1}
        };

        formValidate("basic_form", rules, true);

        var rules = {
            annextype: {minlength: 1, maxlength: 20},
            annexname: {minlength: 1, maxlength: 100},
            remark: {maxlength: 2000}
        };

        formValidate("attach_form", rules, true);

        fastInput();
    });
</script>
</html>
